<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class pmsmodel {

	var $db;
	var $base;

	function pmsmodel(&$base) {
		$this->base = $base;
		$this->db = $base->db;
	}
	
	function check_recipient($sendto,$type){
		$send=explode(',',$sendto);
		$send=array_unique($send);
		sort($send);
		$num=count($send);
		$sendto=str_replace(",","','",$sendto);
		$select="SELECT username,uid FROM ".DB_TABLEPRE."user WHERE username IN ('$sendto')";
		$query=$this->db->query($select);
		if($this->db->num_rows($query)==$num && $type!=1) return '';
		while($result=$this->db->fetch_array($query)){
			$uid[]=$result['uid'];
			$index=array_keys($send,$result['username']);
			unset($send[$index[0]]);
		}
		if($type==1){
			return $uid;
		}else{
			return implode(',',$send);
		}
	}

	function send_message($sendto,$subject,$content,$from,$fromid,$isdraft){
		$uid=$this->check_recipient($sendto,1);
		$isdraft=($isdraft=='on')?1:0;
		$num=count($uid);
		if($num>0){
			$pms="INSERT INTO ".DB_TABLEPRE."pms (`from`,fromid,drafts,toid,subject,message,`time`,`new`) VALUES ('".$from."','".$fromid."','".$isdraft."','".$uid[0]."','".$subject."','".$content."','".$this->base->time."',1),";
			for($i=1;$i<$num;$i++){
				$pms.= "('".$from."','".$fromid."','".$isdraft."','".$uid[$i]."','".$subject."','".$content."','".$this->base->time."',1),";	
			}
			$query=$this->db->query(substr($pms,0,-1));
		}
		return $query;
	}
	
	function add_blacklist($blacklist,$uid){
		return($this->db->query("REPLACE INTO ".DB_TABLEPRE."blacklist (uid,blacklist) VALUES('$uid','$blacklist')"));
	}
	
	function get_blacklist($uid){
		$user=$this->db->fetch_first("SELECT blacklist FROM ".DB_TABLEPRE."blacklist WHERE uid='".$uid."'");
		return $user['blacklist'];
	}
	
	function get_box($uid,$start,$limit,$type){
		if($type==''){
			$blacklist=$this->get_blacklist($uid);
			if($blacklist =="[ALL]"){
				return '';
			}else{
				$blackuser=str_replace(",","','",$blacklist);
				$query=$this->db->query("SELECT * FROM ".DB_TABLEPRE."pms WHERE toid='".$uid."' AND delstatus<>2 AND  drafts !=1 AND `from` NOT IN ('$blackuser') ORDER BY id DESC LIMIT $start,$limit");
			}
		}else{
			$isdraft=($type=='outbox')?'a.drafts<>1':'a.drafts=1';
			$query=$this->db->query("SELECT a. * ,b.username FROM ".DB_TABLEPRE."pms a,".DB_TABLEPRE."user b WHERE a.fromid='".$uid."' AND a.toid=b.uid AND a.delstatus<>1 AND $isdraft ORDER BY a.id DESC LIMIT $start,$limit");
		}
		while($pms=$this->db->fetch_array($query)){
			$pms['time']=$this->base->date($pms['time']);
			$pmslist[]=$pms;
		}
		return $pmslist;
	}
	
	function get_pms($id){
		$pms=$this->db->fetch_first("SELECT a. * ,b.username FROM ".DB_TABLEPRE."pms a,".DB_TABLEPRE."user b WHERE a.toid=b.uid AND a.id=$id");
		return $pms;
	}
	
	function update_pms($id,$type){
		$pms=$this->get_pms($id);
			if($pms['delstatus']==$type){
				$result=$this->remove($id);
			}else{
				$type=($type>1)?1:2;
				$result=$this->db->query("UPDATE ".DB_TABLEPRE."pms SET delstatus='$type' WHERE id=$id");
			}
		return $result;
	}
	
	function remove($id){
		return($this->db->query("DELETE FROM ".DB_TABLEPRE."pms WHERE id=$id"));
	}
	
	function get_totalpms($uid,$type){
		if($type==''){
			$blacklist=$this->get_blacklist($uid);
			if($blacklist =="[ALL]"){
				return '0';
			}else{
				$blackuser=str_replace(",","','",$blacklist);
				$total=$this->db->fetch_first("SELECT COUNT(*) num FROM ".DB_TABLEPRE."pms WHERE toid='$uid' AND delstatus<>2 AND  drafts !=1 AND `from` NOT IN ('$blackuser')");	
			}
		}else{
			$isdraft=($type=='outbox')?'drafts<>1':'drafts=1';
			$total=$this->db->fetch_first("SELECT COUNT(*) num FROM ".DB_TABLEPRE."pms WHERE fromid='$uid' AND delstatus!=1 AND $isdraft");
		}
		return $total['num'];
	}
	
	function remove_blacklist($uid){
		return($this->db->query("DELETE FROM ".DB_TABLEPRE."blacklist WHERE uid=$uid"));
	}
	
	function set_read($id){
		return($this->db->query("UPDATE ".DB_TABLEPRE."pms SET `new`=0 WHERE id=$id"));
	}
}
?>